package com.sunshine.springbootshirothymeleaf.utils;

import java.util.Random;

//随机盐的生成
public class SaltUtils {

    /**
     * 生成salt的静态方法
     * @param n salt的位数
     * @return 返回salt字符串
     */
    public static String getSalt(int n) {
        char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890!@#$%^&*()".toCharArray();
        StringBuilder sb = new StringBuilder();
        //从chars中随机获取指定位数的字符并拼接成字符串
        for (int i = 0; i < n; i++) {
            char aChar = chars[new Random().nextInt(chars.length)];
            sb.append(aChar);
        }
        return sb.toString();
    }
}
